// pages/myself/commission/qrcode.js
var Zan = require('../../../dist/index.js');
let network = null;
let utils = null;
let api = null;
const app = getApp();

var timer;
var temp = 0;

Page(Object.assign({}, Zan.TopTips, Zan.Toast, {

  /**
   * 页面的初始数据
   */
  data: {
    saleInfo: null,
    balance: 0,
    notice: [],
    index: 0,
    showDialog: false,
    FxshowDialog:false,
    count: 0, //成功邀请
    income1:0, //预计收益
    income2:0  //实际收益
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    network = new app.NetWorkUtils();
    utils = new app.Utils();
    api = new app.Api();

    wx.showShareMenu({
      withShareTicket: true //要求小程序返回分享目标信息
    })
    // if (app.globalData.userId) {
    //   this.setData({
    //     qrcode: app.globalData.userAndTenantInfo.qrCode
    //   })
    // } else {
    //   // 由于 openId 是网络请求，可能会在 Page.onLoad 之后才返回
    //   // 所以此处加入 callback 以防止这种情况
    //   app.userAndTenantInfoCallback = res => {
    //     this.setData({
    //       qrcode: res.qrCode
    //     })
    //   }
    // }

    this.onPullDownRefresh();
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.stopPullDownRefresh();
    network._post_json(this, api.Userexea, {}, yjsuccess);
    network._post_json(this, api.Usereain, {}, sjsuccess);
    network._post_json(this, api.getSaleInfo, {}, infosuccess);
    network._post_json(this, api.getSaleRule, {}, success);
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      path: '/pages/home/Home?shared=' + app.globalData.userInfo.id,
      imageUrl: this.data.saleInfo.qrCode,
      success: function (res) {
        console.log(app.globalData.userInfo.id);
        //转发成功
        // if (res.shareTickets) {
        //   wx.getShareInfo({
        //     shareTicket: res.shareTickets,
        //     success: function (res) {
        //       network._post_json(this, api.updateFxcs, { directShareNums: 0, directShareQunNums: 1 }, fxcsSuccess);
        //     },
        //     fail: function (res) {
        //       // 转发失败
        //       network._post_json(this, api.updateFxcs, { directShareNums: 1, directShareQunNums: 0 }, fxcsSuccess);
        //     }
        //   })
        // } else {
        //   network._post_json(this, api.updateFxcs, { directShareNums: 1, directShareQunNums: 0 }, fxcsSuccess);
        // }

      },
      fail: function (res) {
        // 转发失败
      }
    }
  },

  /**
   * 弹出框开关
   */
  toggleDialog() {
    this.setData({
      showDialog: !this.data.showDialog
    })
  },
  FxtoggleDialog() {
    this.setData({
      FxshowDialog: !this.data.FxshowDialog
    })
  },

  /**
   * 预计收益
   */
  goExpectedEarnings: function (e) {
    wx.navigateTo({
      url: '/pages/myself/commission/ExpectedEarnings'
    })
  },

  /**
   * 实计收益
   */
  goActuallyAchieve: function (e) {
    wx.navigateTo({
      url: '/pages/myself/commission/ActuallyAchieve'
    })
  },

  /**
   * 成功邀请
   */
  goDistributioner: function (e) {
    wx.navigateTo({
      url: '/pages/myself/commission/Distributioner'
    })
  },
  /**
   * 提现
   */
  gotixian: function (e) {
    wx.navigateTo({
      url: '/pages/myself/commission/Commtixian'
    })
  },

  sharedImg: function (e) {
    var img = e.currentTarget.dataset.img;
    var that = this;
    wx.showLoading({
      title: '',
    })
    //http://osnmq3793.bkt.clouddn.com
    var downloadTask = wx.downloadFile({
      url: api.upload + '?url=' + img,
      success: function (res) {
        wx.hideLoading();
        wx.saveImageToPhotosAlbum({
          filePath: res.tempFilePath,
          success(res) {
            wx.hideLoading();
            that.showZanToast('保存成功');
          },
          fail(res) {
            wx.hideLoading();
          }
        })
      }
    })
  }

}))

var success = function (that, res) {
  if (res.data.data) {
    that.setData({
      saleInfo: res.data.data
    })
  }
}
var yjsuccess = function(that,res){
  if (res.data.data) {
    that.setData({
      income1: res.data.data.yujimoney
    })
  }
}
var sjsuccess = function (that, res) {
  if (res.data.data) {
    that.setData({
      income2: res.data.data.shijimoney
    })
  }
}
var infosuccess = function (that, res) {
  if (res.data.data) {
    that.setData({
      count: res.data.data.length
    })
  }
}

var fxcsSuccess = function (that, res) {
  that.showZanToast('分享成功');
}
